package com.link.base.base.message.service;

import javax.annotation.Resource;

import com.link.base.base.message.dao.mybatis.mapper.MessagePublishMapper;
import com.link.base.base.message.dao.mybatis.mapper.MessageReceiverMapper;
import com.link.base.base.message.model.MessagePublish;
import com.link.base.base.message.model.MessageReceiver;
import com.link.base.base.message.push.model.PushInfoInsert;
import com.link.base.base.message.push.service.PushService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消息发布-维护发布消息 
 *
 * @author ouyangzhenqiang
 * @date 2019/07/25 13:16
 */
@Service
public class MessagePublishServiceImpl extends BasicServiceImpl<MessagePublish> implements MessagePublishService {
    @Resource
    private MessagePublishMapper messagePublishMapper;
    @Resource
    private MessageReceiverMapper messageReceiverMapper;
    @Resource
    private PushService pushService;

    @Override
    public BasicMapper<MessagePublish> getBasicMapper() {
        return messagePublishMapper;
    }

    @Override
    public void publishMessage(Long id) throws Exception {
        MessagePublish messageParam = new MessagePublish();
        messageParam.setId(id);
        messageParam = messagePublishMapper.queryById(messageParam);
        MessageReceiver messageReceiver = new MessageReceiver();
        messageReceiver.setMsgId(id);
        List<MessageReceiver> list = messageReceiverMapper.getReceiverListByMsgId(messageReceiver);
        String msgStatus = messageParam.getMsgStatus();
        if ("Published".equals(msgStatus)) {
            throw new BasicServiceException("该消息已经发布！");
        }
        if (list == null || list.size() == 0) {
            throw new BasicServiceException("请先维护消息接收人！");
        }
        String pushType = messageParam.getPushType();
        String[] pushTypeList = pushType.split(",");
        String appPushFlag = "N";
        String webPushFlag = "N";
        for (String type: pushTypeList) {
            if ("MobileRemind".equals(type)) {
                appPushFlag = "Y";
            } else if ("SystemRemind".equals(type)) {
                webPushFlag = "Y";
            }
        }
        if ("N".equals(appPushFlag) &&  "N".equals(webPushFlag)) {
            throw new BasicServiceException("请维护消息提醒方式！");
        }

        // 创建提醒
        String pushTitle = messageParam.getMsgTitle();
        String pushNote = messageParam.getMsgNote();
        String pushDate = messageParam.getPushDate();
        List<Long> userList = new ArrayList<>();
        for (MessageReceiver item: list) {
            userList.add(item.getUserId());
        }
        PushInfoInsert pushInfoInsert = new PushInfoInsert();
        pushInfoInsert.setPushTitle(pushTitle);
        pushInfoInsert.setPushNote(pushNote);
        pushInfoInsert.setRemindDt(pushDate);
        pushInfoInsert.setAppPushFlag(appPushFlag);
        pushInfoInsert.setWebPushFlag(webPushFlag);
        pushInfoInsert.setUserList(userList);
        pushService.messageInsert(pushInfoInsert);

        // 发布成功
        messageParam.setMsgStatus("Published");
        messageParam.setPublishDate(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        messagePublishMapper.msgStatusUpdate(messageParam);
    }
}

